class BaseSentinelController extends AIController;

var BaseSentinel sent;
var bool bEnemyIsVisible;

replication
{
    reliable if(Role==Role_Authority)
        sent;
}

function Possess(Pawn aPawn)
{
	Super.Possess(aPawn);

	if(aPawn == None)
		Destroy();

	if(!aPawn.IsA('BaseSentinel'))
	{
		warn("Tried to possess something that was not a Sentinel.");
		aPawn.UnPossessed();
		Destroy();
		return;
	}

	sent = BaseSentinel(aPawn);

	PlayerReplicationInfo = Spawn(class'SentinelPlayerReplicationInfo', Self,,vect(0,0,0),rot(0,0,0));

	if(sent.InstigatorController.PlayerReplicationInfo.Team != None)
		PlayerReplicationInfo.Team = sent.InstigatorController.PlayerReplicationInfo.Team;

	PlayerReplicationInfo.SetPlayerName("");
	PlayerReplicationInfo.bWelcomed = true; //Possibly fixes multiple entry messages?
	sent.PlayerReplicationInfo = PlayerReplicationInfo;
}

function bool LOSTo(actor Other)
{
    local vector HitLocation,HitNormal;
    local Actor tracer,traced;
    tracer=sent;
    if(other==none) return false;
    //if(lineOfSightTo(Other)==false)
    //    return false;
    //if(sentinel(tracer)!=none && sentinel(tracer).Upgrade.GetUpgrade("Molecular Shield")!=none && SentinelShield(sentinel(tracer).Upgrade.GetUpgrade("Molecular Shield")).ShieldSphere!=none)
    //    tracer=SentinelShield(sentinel(tracer).Upgrade.GetUpgrade("Molecular Shield")).ShieldSphere;
    //traced=tracer.trace(HitLocation,HitNormal,Other.Location,tracer.Location,true);
    foreach TraceActors(class'Actor', traced, HitLocation, HitNormal, Other.Location, tracer.Location)
	{
		if(traced == None || Volume(traced)!=none)
			continue;

		if(ShieldSphere(traced)!=none && ShieldSphere(traced).Shield!=none && ShieldSphere(traced).Shield.Sentinel==sent)
            continue;

		//Don't hit shield
		if(BigShieldSphere(traced)!=none)
            continue;

        break;
	}
    if(traced == none  || (tfstation(traced)!=none && tfstation(traced).MyTeam != basesentinel(tracer).myteam) || (pawn(traced)!=none && pawn(traced).PlayerReplicationInfo.Team.TeamIndex != basesentinel(tracer).myteam))
        return true;
    return false;
}

function PawnDied(Pawn P)
{
	Destroy();
}

simulated event Destroyed()
{
	if(Role < ROLE_Authority)
		return;

	if(PlayerReplicationInfo != None)
	{
		//Make sure the game is ended if a Sentinel is the last thing killed in Invasion/LMS.
		PlayerReplicationInfo.bOutOfLives = true;
		Deathmatch(Level.Game).CheckMaxLives(PlayerReplicationInfo);

		PlayerReplicationInfo.Destroy();
	}

	RemoveController();
}